﻿using System;
using System.Data;
using Swirebev.PhotoDownload.Model;
using System.Data.SqlClient;
using Swirebev.PhotoDownload.Common;
using System.Reflection;
using System.Collections.Generic;
//using Qiniu.Storage;
using Swirebev.PhotoDownload.Common;
using Swirebev.PhotoDownload.DAL;
using Swirebev.PhotoDownload.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;

namespace Swirebev.PhotoDownload.DAL
{
   public class photoTime
    {

       public static List<PhotoMsessage> GetPhotoInfo()
       {
           try
           {
               string _connectionString = Settings.Instance.connectionStringTime;

               using (SqlConnection connection = new SqlConnection(_connectionString))
               {
                   connection.Open();
                   SqlCommand command = new SqlCommand();
                   command.Connection = connection;
                   command.CommandText = "RRunner.SBIPlant.P_RRunner_GetPhotoUrlAndName_Reload";
                   command.CommandType = System.Data.CommandType.StoredProcedure;

                   command.Parameters.Add("@photo_date", SqlDbType.NVarChar);
                   command.Parameters.Add("@end_date", SqlDbType.NVarChar);

                   // datetime epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000



                   //获取时间
                   DateTime WQ = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                   TimeSpan dt = new TimeSpan(Settings.Instance.putTime);  //item.PutTime
                   DateTime re = new DateTime();
                   re = WQ.Add(dt);
                   string time = re.ToString("yyyy-MM-dd HH:mm:ss.fff");
                   command.Parameters["@photo_date"].Value = time;

                   DateTime WQ2 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                   TimeSpan dt2 = new TimeSpan(Settings.Instance.endTime);  //item.PutTime
                   DateTime re2 = new DateTime();
                   re2 = WQ2.Add(dt2);
                   string time2 = re2.ToString("yyyy-MM-dd HH:mm:ss.fff");
                   command.Parameters["@end_date"].Value = time2;



                   SqlDataAdapter dp = new SqlDataAdapter(command);
                   DataSet ds = new DataSet();

                   dp.Fill(ds);


                   


              

                   List<PhotoMsessage> photoList = new List<PhotoMsessage>();
               

                   foreach (DataRow mDr in ds.Tables[0].Rows)
                   {
                       PhotoMsessage pm = new PhotoMsessage();
                       pm.PhotoName = mDr[0].ToString();
                       pm.PhotoUrl = mDr[1].ToString();
                       pm.PhotoDate = mDr[2].ToString();  //截止日期
                       photoList.Add(pm);
                   }

                   if (photoList.Count == 0)
                   {

                       Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "  which GEI From RR is Empty collection");
                       System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);

                       tt.Kill();

                   }



                   // Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "SQL SERVER:success to save [" + photo.PhotoName + "]");
                   return photoList;
               }
           }
           catch (Exception ex)
           {
               Log.Error(MethodBase.GetCurrentMethod().DeclaringType, "SQL SERVER RR failed to connect", ex);
               return null;
           }
        
        }

    }
}
